export const communal = {
  data() {
    return {
      url: null,
      addUrl: null,
      editUrl: null,
      config: {
        page: 1,
        limit: 10
      },
      total: 0,
      loading: false,
      tableData: [],
      form: {},
      isReadonly: true,
      statusType: 'add',
      isShow: false,
      checkCode: null
    }
  },
  computed: {
    // //滚动监听
    // defaultOption() {
    //   return {
    //     step: 0.5, // 数值越大速度滚动越快
    //     limitMoveNum: 5, // 开始无缝滚动的数据量 this.dataList.length
    //     hoverStop: true, // 是否开启鼠标悬停stop
    //     direction: 1, // 0向下 1向上 2向左 3向右
    //     openWatch: true // 开启数据实时监控刷新dom
    //   }
    // }
  },
  methods: {
    //表头背景色
    thaderBack() {
      return { background: '#F5F7FA', color: '#000' }
    },
    closeDetails() {
      this.resetFormDetails('FormDetails')
      this.isShow = false
    },
    //接口请求
    getJson(val) {
      return JSON.stringify(val)
    },
    getUrl() {
      return this.url + '?pageNum=' + this.config.page + '&pageSize=' + this.config.limit
    },
    getList() {
      this.loading = true
      this.tableData = []
      this.getSub().then(res => {
        this.loading = false
        this.total = res.total
        this.tableData = res.rows
      })
    },
    getSub: function() {
      return this.$http.post(this.getUrl(), this.getJson(this.form))
    },
    //分页
    handleSizeChange(limit) {
      this.config.limit = limit
      this.getList()
    },
    handleCurrentChange(page) {
      this.config.page = page
      this.getList()
    },
    //搜索
    handleQuery() {
      this.loadding = true
      this.config.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetForm(form) {
      if (this.$refs[form] !== undefined) {
        this.$refs[form].resetFields()
      }
      this.getList()
    },
    resetFormDetails(form) {
      if (this.$refs[form] !== undefined) {
        this.$refs[form].resetFields()
      }
    },
    handleAdd() {
      this.statusType = 'add'
      this.FormDetails = {}
      this.isShow = true
    },
    //提交按钮
    submitSend: function() {
      this.$refs['FormDetails'].validate(valid => {
        if (valid) {
          this.loading = true
          if (this.statusType === 'edit') {
            this.$http.post(this.editUrl, this.FormDetails).then(response => {
              this.loading = false
              if (response.code === 200) {
                this.$message({ type: 'success', message: '更新成功！' })
                this.isShow = false
                this.getList()
              }
            })
          } else {
            this.$http.post(this.addUrl, this.getJson(this.FormDetails)).then(response => {
              this.loading = false
              if (response.code === 200) {
                this.$message({ type: 'success', message: '新增成功！' })
                this.isShow = false
                this.getList()
              }
            })
          }
        }
      })
    },
    getDate() {
      // const year = new Date().getFullYear()
      // const month = new Date().getMonth() + 1 < 10 ? '0' + (new Date().getMonth() + 1) : new Date().getMonth() + 1
      // const date = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate()
      const hh = new Date().getHours() < 10 ? '0' + new Date().getHours() : new Date().getHours()
      const mm = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()
      const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()
      return hh + ':' + mm + ':' + ss
    },
    //随机数
    createCode() {
      let code = ''
      var codeLength = 6
      var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
      for (var i = 0; i < codeLength; i++) {
        var index = Math.floor(Math.random() * 10)
        code += random[index]
      }
      this.checkCode = code
    }
  },
  created() {
    this.getList()
    this.createCode()
  }
}
